window.exports = {
  "CaseFormat": {
    mode: "list",
    args: {
      placeholder: "请输入要转换的字符串",
      enter: (action, callbackSetList) => callbackSetList(getCaseFormatList(action.payload || "")),
      search: (action, searchWord, callbackSetList) => callbackSetList(getCaseFormatList(searchWord)),
      select: (action, itemData, callbackSetList) => {
        window.utools.outPlugin()
        const text = itemData.data
        window.utools.copyText(text)
        window.utools.hideMainWindowPasteText(text)
      },
    },
  },
}

window.utools.onMainPush(onMainPushShow, onMainPushSelect)

function onMainPushShow({code, type, payload}) {
  const list = getCaseFormatList(payload)
  const {startIndex, endIndex} = payload.toString().endsWith("  ")
    ? {startIndex: 5, endIndex: 10}  // 第二页
    : {startIndex: 0, endIndex: 5}
  const result = list.map((item, index) => {
    return {
      icon: "logo.png",
      text: `${index + 1}/${list.length} → ${item.data}`,
      title: `格式：${item.description}。点击可进行复制`,
      data: item.data,
    }
  }).filter((item, index) => startIndex <= index && index < endIndex)
  result.push({
    icon: "logo.png",
    text: startIndex === 0 ? '【添加空格查看下一页】': '【删除空格查看上一页】',
    title: '',
    data: '',
  })
  return result
}

function onMainPushSelect({code, type, payload, option}) {
  option.data && utools.hideMainWindowPasteText(option.data)
}

function getCaseFormatList(str) {
  const string = str.trim()
  if (!string) {
    return []
  }
  const wordList = splitWords(string.toString().trim())
  // kebab-case
  const str1 = wordList.join("-").toLowerCase()
  // snake_case
  const str2 = wordList.join("_").toLowerCase()
  // SNAKE_CASE
  const str3 = wordList.join("_").toUpperCase()
  // CamelCase
  const str4 = wordList.map(word => word.charAt(0).toUpperCase() + word.slice(1)).join("")
  // camelCase
  const str5 = wordList.map((word, index) => index === 0 ? word : word.charAt(0).toUpperCase() + word.slice(1)).join("")
  // Camel Case
  const str6 = wordList.map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(" ")
  // space case
  const str7 = wordList.join(" ")
  // 大写
  const str8 = string.toUpperCase()
  // 小写
  const str9 = string.toLowerCase()
  return [
    {icon: "logo.png", title: str8, description: "大写", data: str8},
    {icon: "logo.png", title: str9, description: "小写", data: str9},
    {icon: "logo.png", title: str5, description: "camelCase", data: str5},
    {icon: "logo.png", title: str4, description: "CamelCase", data: str4},
    {icon: "logo.png", title: str6, description: "Camel Case", data: str6},
    {icon: "logo.png", title: str7, description: "space case", data: str7},
    {icon: "logo.png", title: str1, description: "kebab-case", data: str1},
    {icon: "logo.png", title: str2, description: "snake_case", data: str2},
    {icon: "logo.png", title: str3, description: "SNAKE_CASE", data: str3},
  ]
}

/**
 * 将字符串分割成单词数组。分割符可以使用 ‘-’、‘-’、‘ ’和大写字母
 */
function splitWords(str) {
  const list = []
  let word = ""
  for (let i = 0; i < str.length; i++) {
    const char = str[i]
    if (char === "-" || char === "_" || char === " ") {
      word && list.push(word.toLowerCase())
      word = ""
    } else if (char === char.toUpperCase()) {
      word && list.push(word.toLowerCase())
      word = char
    } else if (i === str.length - 1) {
      word += char
      word && list.push(word.toLowerCase())
    } else {
      word += char
    }
  }
  return list
}
